package com.spring.format.code.generation.util;

import org.apache.commons.lang3.StringUtils;

import com.spring.format.code.generation.constants.Constants;

/**
 * 公用代码 帮助类，很多地方的代码是一样的，放在这里以后改的时候，容易
 * 
 * @author Administrator
 *
 */
public class CommonCodeUtil {

	public static final String LOGIN_VO =" loginVO ";
	private CommonCodeUtil() {

	}
	
	public static String paramVO(String voName) {
		StringBuilder methodStr = new StringBuilder();
		// 查询数量
		methodStr.append(voName).append(" paramVO ");
		return methodStr.toString();
	}
	public static String paramVOList(String voName) {
		StringBuilder methodStr = new StringBuilder();
		// 查询数量
		methodStr.append(" List<").append(voName).append("> voList ");
		return methodStr.toString();
	}
	
	public static String methodRemark(String remark) {
		StringBuilder methodStr = new StringBuilder();
		// 查询数量
		methodStr.append(" /**  \n ").append(remark).append("  */ \n ");
		return methodStr.toString();
	}
	public static String importClass() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("import java.util.List; \n")
		.append("import java.util.Date; \n")
		.append("import com.sx.cache.Cache; \n")
		.append("import org.springframework.stereotype.Service; \n ")
		.append("import com.sx.common.util.json.JackJsonUtil; \n")
		.append("import com.sx.common.util.BeanUtils;\n")
		.append("import com.sx.common.util.NumberUtil;\n")
		.append("import org.slf4j.Logger;\n")
		.append("import org.slf4j.LoggerFactory;\n");
		return methodStr.toString();
	}

	/** 异常部分通用代码 **/
	public static String serviceShopCommonEx(String remark) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" 			}catch(Exception ex){\n").append(" 				log.error(\"").append(remark)
				.append("error={}\",ex); \n")
				.append(" 				resInfo.setMessage(ResInfoCode.RIC_HTTPSTATUS_500.getMessage()); \n")
				.append("                return resInfo;  \n ").append(" 			} \n")
				.append("        resInfo.setCode(ResInfoCode.RIC_SUCCEES); \n").append("		return resInfo; \n")
				.append("  } \n \n \n ");
		return methodStr.toString();
	}
	/** 异常部分通用代码 **/
	public static String serviceCommonEx(String remark) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" 			}catch(Exception ex){\n").append(" 				log.error(\"").append(remark)
				.append("error={}\",ex); \n")
				.append(" 				 resultVO.setMsg(\"").append(remark).append("失败\"); \n")
				.append(" resultVO.setSuccess(false);  \n ")
				.append("                return resultVO;  \n ").append(" 			} \n")
				.append("		return resultVO; \n")
				.append("  } \n \n \n ");
		return methodStr.toString();
	}

	/** 方法内 result 声明 通用代码 **/
	public static String resultShopStatement(String returnType, String voName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(Constants.RESINFO_VO).append(" < ");
		// 根据返回类型拼接
		switch (returnType) {
		case "list":
			methodStr.append("List<").append(voName).append("> ");
			break;
		case "vo":
			methodStr.append(voName);
			break;
		case "int":
			methodStr.append("Integer");
			break;
		default:
			methodStr.append("?");
			break;
		}
		methodStr.append(" > resInfo = new ").append(Constants.RESINFO_VO).append("<>(ResInfoCode.RIC_FAIL); \n");
		return methodStr.toString();
	}
	/** 方法内 result 声明 通用代码 **/
	public static String resultStatement(String returnType, String voOrFormName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(Constants.RESINFO_VO).append(" < ");
		// 根据返回类型拼接
		switch (returnType) {
		case "list":
			methodStr.append("List<").append(voOrFormName).append("> ");
			break;
		case "vo":
			methodStr.append(voOrFormName);
			break;
		case "form":
			methodStr.append(voOrFormName);
			break;
		case "int":
			methodStr.append("Integer");
			break;
		default:
			methodStr.append("?");
			break;
		}
		methodStr.append(" > resultVO = new ").append(Constants.RESINFO_VO).append("<>(); \n");
		return methodStr.toString();
	}

	/** 方法内 dao 层引用 公用代码 **/
	public static String daoMethodCall(String daoImplName, String methodName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(daoImplName).append(".").append(methodName).append(" (po); \n");
		return methodStr.toString();
	}
	/** 方法内 service 层引用 公用代码 **/
	public static String serviceMethodCall(String serviceImplName, String methodName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(serviceImplName) .append(".").append(methodName).append("(paraForm); \n  ");
		return methodStr.toString();
	}
	/** 方法内 service 层引用 公用代码 **/
	public static String serviceMethodCall(String serviceImplName, String methodName,String paramStr) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(serviceImplName) .append(".").append(methodName).append("(").append(paramStr).append("); \n ");
		return methodStr.toString();
	}
	/** po new 方法 **/
	public static String poNew(String poName) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(poName).append(" po = new ").append(poName).append("(); \n");
		return methodStr.toString();
	}
	/** logger 声明 方法 **/
	public static String logStatement() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" /*** 日志*/ \n ")
		.append("	private Logger log = LoggerFactory.getLogger(this.getClass());\n ");
		return methodStr.toString();
	}
	/** redis 引用 声明 方法 **/
	public static String redisStatement() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("     @Resource(name=\"redisCache\") \n ")
		.append("		protected Cache cache; \n ");
		return methodStr.toString();
	}
	/** try Begin  **/
	public static String tryBegin() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" try { \n ");
		return methodStr.toString();
	}
	/** List<PO>  **/
	public static String listPO (String poName ) {
		return " List<"+poName+"> poList = ";
	}
	public static String nullReturn(String param) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" if( null == ").append(param).append(" ){ \n")
		.append("  log.debug(\"").append(param).append(":{} 参数为空\",").append(param).append("); \n ")
		.append("	 resultVO.setMsg(\"参数为空\"); \n	 ")
		.append("    resultVO.setSuccess(false); \n ")
		.append("	 return resultVO;  \n	 ")
		.append("	} \n ");
		return methodStr.toString();
	}
	public static String nullReturnShop(String param) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" if( null == ").append(param).append(" ){ \n")
		.append("  log.debug(\"").append(param).append(":{} 参数为空\",").append(param).append("); \n ")
		.append("	 resInfo.setMessage(\"参数为空\"); \n	 ")
		.append("	 return resInfo;  \n	 ")
		.append("	} \n ");
		return methodStr.toString();
	}
	/** id null return 方法 **/
	public static String idNullReturnShop() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(nullReturnShop("id"));
		return methodStr.toString();
	}
	/** vo null return 方法 **/
	public static String voNullReturnShop() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(nullReturnShop("vo"));
		return methodStr.toString();
	}
	/** voList null return 方法 **/
	public static String voListNullReturnShop() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(nullReturnShop("voList"));
		return methodStr.toString();
	}
	/** 方法结束  **/
	public static String methodEnd() {
		return methodEnd(null);
	}
	/** 方法结束  **/
	public static String methodEnd(String remark) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" } ");
		if(StringUtils.isNotBlank(remark)) {
			methodStr.append(" // ").append(remark);
		}
		methodStr.append(" \n \n \n ");
		return methodStr.toString();
	}
	/** 只是only **/
	public static String returnOnly() {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append(" return ");
		return methodStr.toString();
	}
	/** biz impl 方法返回对象 通用代码 **/
	public static String implMethodResult(String returnType, String voName, String methodName, String paramStr) {
		return methodResult(returnType,voName,methodName,paramStr);
	}
	/** biz impl 方法返回对象 通用代码 **/
	public static String implMethodResultLogin(String returnType, String voName, String methodName, String paramStr) {
		return methodResult(returnType,voName,methodName,paramStr+", "+Constants.CONSUMER_LOGIN_VO+LOGIN_VO);
	}
	/** service 方法返回对象 通用代码 **/
	public static String apiMethodResult(String returnType, String voName, String methodName, String paramStr) {
		return methodStatement(returnType,voName,methodName,paramStr);
	}
	/** biz方法返回对象 通用代码 **/
	public static String apiMethodResultLogin(String returnType, String voName, String methodName, String paramStr) {
		return methodStatement(returnType,voName,methodName,paramStr+", "+Constants.CONSUMER_LOGIN_VO+LOGIN_VO);
	}
	/** 方法返回对象 通用代码 **/
	public static String methodResult(String returnType, String voName, String methodName, String paramStr) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("    @Override   \n")
		.append(resultCommon(returnType,voName,methodName,paramStr))
		.append("{ \n ");
		return methodStr.toString();
	}
	/** 接口内方法声明  **/
	public static String methodStatement(String returnType, String voName, String methodName, String paramStr) {
		StringBuilder methodStr = new StringBuilder();
		//为空 默认为vo
		if(StringUtils.isBlank(paramStr)) {
			paramStr = voName +" vo ";
		}
		methodStr.append(resultCommon(returnType,voName,methodName,paramStr )).append(";\n");
		return methodStr.toString();
	}
	/** 接口内方法声明  **/
	public static String resultCommon(String returnType, String voName, String methodName, String paramStr) {
		StringBuilder methodStr = new StringBuilder();
		methodStr.append("	public ").append(Constants.RESINFO_VO).append("<");
		// 根据返回类型拼接
		switch (returnType) {
		case "list":
			methodStr.append("List<").append(voName).append("> ");
			break;
		case "vo":
			methodStr.append(voName);
			break;
		case "form":
			methodStr.append(voName);
			break;
		case "int":
			methodStr.append("Integer");
			break;
		default:
			methodStr.append("?");
			break;
		}
		methodStr.append("> ").append(methodName).append("(").append(paramStr).append(") ");
		return methodStr.toString();
	}
}
